﻿using System;
using SharpSqlAnalyzer.nodes;
using SharpSqlAnalyzer.pp.processor.type.comm;
using SharpSqlAnalyzer.pp.utils;
using SharpSqlAnalyzer.stmt;

namespace SharpSqlAnalyzer.pp.processor.type.plsql
{
	// Token: 0x02000239 RID: 569
	public class PlsqlIfStmtBEProcessor : AbstractProcessor
	{
		// Token: 0x060012DF RID: 4831 RVA: 0x0031B314 File Offset: 0x00319514
		public override void process(TParseTreeNode nodeArg)
		{
			TIfStmt tifStmt = nodeArg as TIfStmt;
			TSourceToken startToken = tifStmt.startToken;
			if (startToken == null)
			{
				return;
			}
			TStatementList elseifStatements = tifStmt.ElseifStatements;
			if (elseifStatements != null && elseifStatements.size() > 0)
			{
				for (int i = 0; i < elseifStatements.size(); i++)
				{
					TElsifStmt telsifStmt = (TElsifStmt)elseifStatements.get(i);
					TSourceToken startToken2 = telsifStmt.startToken;
					TSourceToken tsourceToken = SourceTokenSearcher.firstNotWhitespaceAndReturnToken(startToken2.container, startToken2.posinlist + 1, telsifStmt.endToken.posinlist);
					if (tsourceToken != null)
					{
						this.wrapStatement(startToken, startToken2, tsourceToken, false);
						TStatementList thenStatements = telsifStmt.ThenStatements;
						if (thenStatements != null && thenStatements.size() > 0)
						{
							for (int j = 0; j < thenStatements.size(); j++)
							{
								TCustomSqlStatement tcustomSqlStatement = thenStatements.get(j);
								TSourceToken startToken3 = tcustomSqlStatement.startToken;
								TSourceToken keywordToken = SourceTokenSearcher.backforwardSearch(tcustomSqlStatement.startToken, 5, "then");
								if (startToken3 != null)
								{
									this.wrapStatement(startToken, keywordToken, startToken3, true);
								}
							}
						}
					}
				}
			}
			TStatementList thenStatements2 = tifStmt.ThenStatements;
			if (thenStatements2 != null && thenStatements2.size() > 0)
			{
				for (int k = 0; k < thenStatements2.size(); k++)
				{
					TCustomSqlStatement tcustomSqlStatement2 = thenStatements2.get(k);
					TSourceToken startToken4 = tcustomSqlStatement2.startToken;
					TSourceToken keywordToken2 = SourceTokenSearcher.backforwardSearch(tcustomSqlStatement2.startToken, 5, "then");
					if (startToken4 != null)
					{
						this.wrapStatement(startToken, keywordToken2, startToken4, true);
					}
				}
			}
			TStatementList elseStatements = tifStmt.ElseStatements;
			if (elseStatements != null && elseStatements.size() > 0)
			{
				for (int l = 0; l < elseStatements.size(); l++)
				{
					TCustomSqlStatement tcustomSqlStatement3 = elseStatements.get(l);
					TSourceToken startToken5 = tcustomSqlStatement3.startToken;
					if (startToken5 != null)
					{
						TSourceToken tsourceToken2 = SourceTokenSearcher.backforwardSearch(tcustomSqlStatement3.startToken, 5, "else");
						if (tsourceToken2 != null)
						{
							this.wrapStatement(startToken, tsourceToken2, startToken5);
						}
					}
				}
			}
			TSourceToken targetToken = SourceTokenSearcher.backforwardSearch(tifStmt.endToken, 5, "end");
			int length = SourceTokenOperator.curColumnNumberVT(startToken);
			SourceTokenOperator.addBefore(this.Option, targetToken, SourceTokenOperator.createReturnSourceToken());
			SourceTokenOperator.addBefore(this.Option, targetToken, SourceTokenOperator.createWhitespaceSourceToken(length));
		}

		// Token: 0x060012E0 RID: 4832 RVA: 0x0031B514 File Offset: 0x00319714
		private void wrapStatement(TSourceToken ifToken, TSourceToken keywordToken, TSourceToken startToken)
		{
			int num = SourceTokenOperator.curColumnNumberVT(ifToken);
			SourceTokenOperator.addBefore(this.Option, keywordToken, SourceTokenOperator.createReturnSourceToken());
			SourceTokenOperator.addBefore(this.Option, keywordToken, SourceTokenOperator.createWhitespaceSourceToken(num));
			int length = num + this.Option.beStyleIfElseSingleStmtIndentSize;
			SourceTokenOperator.addBefore(this.Option, startToken, SourceTokenOperator.createReturnSourceToken());
			SourceTokenOperator.addBefore(this.Option, startToken, SourceTokenOperator.createWhitespaceSourceToken(length));
		}

		// Token: 0x060012E1 RID: 4833 RVA: 0x0031B57C File Offset: 0x0031977C
		private void wrapStatement(TSourceToken ifToken, TSourceToken keywordToken, TSourceToken startToken, bool wrapstartToken)
		{
			int num = SourceTokenOperator.curColumnNumberVT(ifToken);
			if (!wrapstartToken)
			{
				SourceTokenOperator.addBefore(this.Option, keywordToken, SourceTokenOperator.createReturnSourceToken());
				SourceTokenOperator.addBefore(this.Option, keywordToken, SourceTokenOperator.createWhitespaceSourceToken(num));
			}
			if (wrapstartToken)
			{
				int length = num + this.Option.beStyleIfElseSingleStmtIndentSize;
				SourceTokenOperator.addBefore(this.Option, startToken, SourceTokenOperator.createReturnSourceToken());
				SourceTokenOperator.addBefore(this.Option, startToken, SourceTokenOperator.createWhitespaceSourceToken(length));
			}
		}
	}
}
